package com.java.forum.d_controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.forum.a_entity.Collection;
import com.java.forum.a_entity.Post;
import com.java.forum.a_entity.Subscribe;
import com.java.forum.a_entity.User;
import com.java.forum.c_service.MailService;
import com.java.forum.c_service.PostService;
import com.java.forum.c_service.TokenService;
import com.java.forum.c_service.UserService;
import com.java.forum.e_util.JsonResult;
import com.java.forum.h_annotation.CurrentUser;
import com.java.forum.h_annotation.NeedToken;
import com.java.forum.h_annotation.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.*;

@RestController
@RequestMapping("user")
public class UserController extends BaseController {
    private UserService userService;
    private TokenService tokenService;
    private MailService mailService;
    private PostService postService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setTokenService(TokenService tokenService) {
        this.tokenService = tokenService;
    }

    @Autowired
    public void setMailService(MailService mailService) {
        this.mailService = mailService;
    }

    @Autowired
    public void setPostService(PostService postService) {
        this.postService = postService;
    }


    @PassToken
    @RequestMapping("login")
    public JsonResult<Map<String, Object>> login(Integer uid, String password) {
        User u = userService.login(uid, password);
        String token = tokenService.getToken(u);
        HashMap<String, Object> map = new HashMap<>();
        map.put("user", u);
        map.put("token", token);
        return new JsonResult<>(ok, map);
    }

    @PassToken
    @RequestMapping("register")
    public JsonResult<Map<String, Integer>> register(User user) {
        Integer res = userService.register(user);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("uid", res);
        return new JsonResult<>(ok, map);
    }

    @PassToken
    @RequestMapping("resetPassword")
    public JsonResult<Void> resetPassword(Integer uid, String email) throws MessagingException, UnsupportedEncodingException {
        String resetPassword = userService.resetPassword(uid, email);
        mailService.sendMail(email, "密码重置", "您的密码已重置为:" + resetPassword + "。使用此密码登录后，请及时修改密码。");
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("getUserInfoByUid")
    public JsonResult<User> getUserInfoByUid(Integer uid) {
        User user = userService.getUserInfo(uid);
        return new JsonResult<>(ok, user);
    }

    @NeedToken
    @RequestMapping("updateUserInfo")
    public JsonResult<Void> updateUserInfo(User u, @CurrentUser User user) {
        user.setAvatar(u.getAvatar());
        user.setGender(u.getGender());
        user.setUsername(u.getUsername());
        userService.updateUserInfo(user);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("updateEmail")
    public JsonResult<Void> updateEmail(String password, String email, @CurrentUser User user) {
        userService.updateUserEmail(user, password, email);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("updatePassword")
    public JsonResult<Void> updatePassword(String oldPassword, String newPassword, @CurrentUser User user) {
        userService.updatePassword(oldPassword, newPassword, user);
        return new JsonResult<>(ok);
    }

    @PassToken
    @RequestMapping("test")
    public JsonResult<Void> test() {
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("SubscribeUser")
    public JsonResult<Map<String, Integer>> SubscribeUser(Subscribe subscribe, @CurrentUser User user) {
        subscribe.setGUid(user.getUid());
        Integer res = userService.SubscribeUser(subscribe);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("sid", res);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("UnSubscribeUser")
    public JsonResult<Void> UnSubscribeUser(Subscribe subscribe, @CurrentUser User user) {
        subscribe.setGUid(user.getUid());
        userService.UnSubscribeUser(subscribe);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("isSubscribe")
    public JsonResult<Map<String, Integer>> isSubscribe(int BUid, @CurrentUser User user) {
        int GUid = user.getUid();
        Integer res = userService.isSubscribe(GUid, BUid);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("isSubscribe", res);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("mySubscribe")
    public JsonResult<List<User>> mySubscribe(Integer uid) {
        Integer my_uid = uid;
        List<Subscribe> my_Subscribe = userService.getSubscribeList(my_uid);
        List<User> user_list = new ArrayList<>();
        for (Subscribe s : my_Subscribe) {
            user_list.add(userService.getUserInfo(s.getBUid()));
        }
        JsonResult<List<User>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setData(user_list);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("mySubscribeNum")
    public JsonResult<Map<String, Integer>> mySubscribeNum(Integer uid) {
        Integer my_uid = uid;
        List<Subscribe> my_Subscribe = userService.getSubscribeList(my_uid);
        Integer res = my_Subscribe.size();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("num", res);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("myBeSubscribed")
    public JsonResult<List<User>> myBeSubscribed(Integer uid) {
        Integer my_uid = uid;
        List<Subscribe> my_BeSubscribed = userService.getBeSubscribedList(my_uid);
        List<User> user_be_list = new ArrayList<>();
        for (Subscribe s : my_BeSubscribed) {
            user_be_list.add(userService.getUserInfo(s.getGUid()));
        }
        JsonResult<List<User>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setData(user_be_list);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("myBeSubscribedNum")
    public JsonResult<Map<String, Integer>> myBeSubscribedNum(Integer uid) {
        Integer my_uid = uid;
        List<Subscribe> my_BeSubscribed = userService.getBeSubscribedList(my_uid);
        Integer res = my_BeSubscribed.size();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("num", res);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("CollectPost")
    public JsonResult<Map<String, Integer>> CollectPost(Collection collection, @CurrentUser User user) {
        collection.setUid(user.getUid());
        Integer res = userService.CollectPost(collection);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("coid", res);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("UnCollectPost")
    public JsonResult<Void> UnCollectPost(Collection collection, @CurrentUser User user) {
        collection.setUid(user.getUid());
        userService.UnCollectPost(collection);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("isCollect")
    public JsonResult<Map<String, Integer>> isCollect(int pid, @CurrentUser User user) {
        Integer res = userService.isCollect(pid, user.getUid());
        HashMap<String, Integer> map = new HashMap<>();
        map.put("isCollect", res);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("myCollect")
    public JsonResult<List<Post>> myCollect(@CurrentUser User user, long date) {
        Date d = new Date(date);
        PageHelper.startPage(1, 10);
        Integer my_uid = user.getUid();
        List<Collection> my_Collection = userService.getCollectionList(my_uid);
        List<Post> Post_list = new ArrayList<>();
        for (Collection c : my_Collection) {
            Post_list.add(postService.getPostInfo(c.getPid()));
            Post_list.get(Post_list.size() - 1).setUser(userService.getUserInfo(Post_list.get(Post_list.size() - 1).getUid()));
        }
        PageInfo<Post> pageInfo = new PageInfo<>(Post_list);
        JsonResult<List<Post>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setData(Post_list);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("myPostNum")
    public JsonResult<Map<String, Integer>> myPostNum(Integer uid) {
        Integer my_uid = uid;
        List<Post> my_Post = postService.getPostByUid(my_uid, new Date());
        Integer res = my_Post.size();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("num", res);
        return new JsonResult<>(ok, map);
    }
}
